﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Yule.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;

namespace BOL.Linq.Distances
{
    public static partial class Similarity
    {
        /// <summary>
        ///        ad - bc
        /// Yule = -------
        ///        ad + bc
        /// </summary>
        /// <see>Yule 1900</see>
        public static double YuleQ(this IEnumerable<bool> first, IEnumerable<bool> second)
        {
            var bc = first.BinaryCardinality(second);

            var ad = bc.Item1 * bc.Item4;
            var bc1 = bc.Item2 * bc.Item3;

            return (ad - bc1) / (double)(ad + bc1);
        }

        /// <summary>
        ///         sqrt(ad) - sqrt(bc)
        /// YuleY = -------------------
        ///         sqrt(ad) + sqrt(bc)
        /// </summary>
        /// <see>Yule 1912</see>
        public static double YuleY(this IEnumerable<bool> first, IEnumerable<bool> second)
        {
            var bc = first.BinaryCardinality(second);

            var sqAd = Math.Sqrt(bc.Item1 * bc.Item4);
            var sqBc = Math.Sqrt(bc.Item2 * bc.Item3);

            return (sqAd - sqBc) / (sqAd + sqBc);
        }
    }
}
